package com.zjj.io.serial;

import java.io.*;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
public class SerialDemo {

    public static void main(String[] args) {
        unserial();
    }

    public static void serial(){
        try (
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
        ){
            Person p = new Person("张三" , "123" , 27);
            p.setHash();
            oos.writeObject(p);
            oos.flush();
            System.out.println("序列化完成");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void unserial(){
        try (
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"))
        ){
            Person p = (Person)ois.readObject();
            System.out.println("p = "+p);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}

class Person implements Serializable {

    private static final long serialVersionUID = 2777914848130492016L;

    private String username;
    private String password;
    private int age;
    private String hash;

    public void setHash() {
        this.hash = genHash();
    }
    public String genHash() {
        String str = username + password + age;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(str.getBytes(Charset.defaultCharset()));
            byte[] bytes = md5.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "error hash";
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        String newHash = this.genHash();
        if(!this.hash.equals(newHash)){
            throw new IllegalArgumentException("对象信息被篡改！");
        }
        System.out.println("this = "+this);
    }

    public Person() {
    }

    public Person(String username, String password, int age) {
        this.username = username;
        this.password = password;
        this.age = age;
    }

    /**
     * 获取
     * @return username
     */
    public String getUsername() {
        return username;
    }

    /**
     * 设置
     * @param username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * 获取
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * 设置
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", hash='" + hash + '\'' +
                '}';
    }
}
